package com.ksyun.seckill.util;

import org.apache.commons.lang3.Validate;

import java.util.Collections;
import java.util.List;
import java.util.Random;

/**
 * @author ksc
 */
public final class RandomUtils {
    public static Random threadLocalRandom() {
        return java.util.concurrent.ThreadLocalRandom.current();
    }

    /**
     * 返回min到max的随机Int,可传入SecureRandom或ThreadLocalRandom.
     */
    public static int nextInt(Random random, int min, int max) {
        Validate.isTrue(max >= min, "Start value must be smaller or equal to end value.");
        if (min < 0) {
            throw new IllegalArgumentException("min必须大于等于0");
        }
        if (min == max) {
            return min;
        }

        return min + random.nextInt(max - min);
    }

    public static int nextInt(int max) {
        return nextInt(threadLocalRandom(), max);
    }
    public static int
    nextInt(Random random, int max) {
        return random.nextInt(max);
    }

    public static int nextInt(int min, int max) {
        return nextInt(threadLocalRandom(), min, max);
    }

    public static <T> List<T> randomSome(List<T> list, int count) {
        Collections.shuffle(list);
        return list.subList(0, count);
    }
}
